package mr;

import java.io.*;

/*
按行切分
**/
public class Demo02Split {
    public static void main(String[] args) throws Exception {


        int sum =sumFileLine ();//获取总行数 1000
        int fileRow=sum/8;//获取每个文件的大小 125
        int index =0;//记录文件下标
        System.out.println (sum );
        System.out.println (fileRow );

        BufferedReader br = new BufferedReader (
                new FileReader ("\\IdeaProjects\\shujiabigdata\\data\\students.txt"));

        BufferedWriter bw = new BufferedWriter (
                new FileWriter ("\\IdeaProjects\\shujiabigdata\\data\\split\\split---" + index));

        int count=0;//记录行数

        String line ;
        while((line=br.readLine ())!=null){


            /**
             * 下一行有数据count+1
             * 当count>fileRow时，写入数据，并关闭当前io流
             * 继续操作开启下一次写入操作
             * count置零，index+1
             *
             * */
            count++;
            if(count>fileRow){
                bw.flush ();
                bw.close ();
                index++;
                count=0;

                bw =new BufferedWriter (
                        new FileWriter ("\\IdeaProjects\\shujiabigdata\\data\\split\\split---" + index));

//                bw.write (line);
//                bw.newLine ();
            }
            /**
             * 最后不足125，直接写入最后一个文件
             *
             */
            bw.write(line);
            bw.newLine();
        }
        bw.flush ();
        bw.close ();
        br.close ();


    }

    public static int sumFileLine() throws Exception{

        BufferedReader br = new BufferedReader (
                new FileReader ("D://\\IdeaProjects\\shujiabigdata\\data\\students.txt"));

        int sum=0;
        String line;
        while((line=br.readLine ())!=null){
            sum++;
        }
        br.close ();
        return sum;

    }
}
